package com.ican.dao.rule;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.ican.common.SystemConstants;
import com.ican.common.hibernate.HibernateDao;
import com.ican.common.hibernate.Page;
import com.ican.domain.rule.ParseRule;

@Repository("parseRuleDao")
public class ParseRuleDao extends HibernateDao<ParseRule, Integer> {
    
    public ParseRule findRuleByCategoryId(Integer categoryId){
        String sql = "select p.* from t_rule_parse_rule p join t_rule_category c on p.id = c.rule_id where c.id = " + 
                categoryId + " and p.data_status = " + SystemConstants.ENTITY_DATE_STATUS_YES;
        
        return (ParseRule) this.createSqlQuery(sql).addEntity(ParseRule.class).uniqueResult();
    }
    
    public Page<ParseRule> findAllRule(Page<ParseRule> page, ParseRule rule){
        List<String> params = new ArrayList<String>();
        if(rule != null){
            if(rule.getRuleDesc() != null && rule.getRuleDesc().length() > 0){
                params.add("ruleDesc like '%" + rule.getRuleDesc() + "%'");
            }
            if(rule.getWebsiteId() != null){
                params.add("websiteId = " + rule.getWebsiteId());
            }
            if(rule.getDataStatus() == 0 || rule.getDataStatus() == 1){
                params.add("dataStatus = " + rule.getDataStatus());
            }
        }
        StringBuffer sb = new StringBuffer("from ParseRule");
        if(params.size() > 0){
            sb.append(" where ");
            for(int i = 0; i < params.size(); i++){
                if(i != 0){
                    sb.append(" and ");
                }
                sb.append(params.get(i));
            }
        }
        sb.append(" order by createTime desc ");
        return this.findPage(page, sb.toString());
    }
    
    public List<ParseRule> findAllRule(ParseRule rule){
        List<String> params = new ArrayList<String>();
        if(rule != null){
            if(rule.getRuleDesc() != null && rule.getRuleDesc().length() > 0){
                params.add("ruleDesc like '%" + rule.getRuleDesc() + "%'");
            }
            if(rule.getWebsiteId() != null){
                params.add("websiteId = " + rule.getWebsiteId());
            }
            if(rule.getDataStatus() == 0 || rule.getDataStatus() == 1){
                params.add("dataStatus = " + rule.getDataStatus());
            }
        }
        StringBuffer sb = new StringBuffer("from ParseRule");
        if(params.size() > 0){
            sb.append(" where ");
            for(int i = 0; i < params.size(); i++){
                if(i != 0){
                    sb.append(" and ");
                }
                sb.append(params.get(i));
            }
        }
        sb.append(" order by createTime desc ");
        return this.find(sb.toString());
    }
    
    public List<ParseRule> findAllRule(Integer websiteId, int dateStatus){
        List<String> params = new ArrayList<String>();
        if(websiteId != null){
            params.add("websiteId = " + websiteId);
        }
        if(dateStatus == 0 || dateStatus == 1){
            params.add("dataStatus = " + dateStatus);
        }
        StringBuffer sb = new StringBuffer("from ParseRule");
        if(params.size() > 0){
            sb.append(" where ");
            for(int i = 0; i < params.size(); i++){
                if(i != 0){
                    sb.append(" and ");
                }
                sb.append(params.get(i));
            }
        }
        sb.append(" order by createTime desc ");
        return this.find(sb.toString());
    }
    
    public void addRule(ParseRule rule){
        rule.setCreateTime(new Date());
        rule.setModifyTime(new Date());
        this.save(rule);
    }
    
    public void modifyRule(ParseRule rule){
        if(rule == null || rule.getId() == null){
            return;
        }
        ParseRule entity = this.get(rule.getId());
        if(entity == null){
            return;
        }
        if(rule.getDataStatus() == 0 || rule.getDataStatus() == 1){
            entity.setDataStatus(rule.getDataStatus());
        }
        if(rule.getRuleDesc() != null){
            entity.setRuleDesc(rule.getRuleDesc());
        }
        if(rule.getDpUrl() != null){
            entity.setDpUrl(rule.getDpUrl());
        }
        if(rule.getNextPage() != null){
            entity.setNextPage(rule.getNextPage());
        }
        if(rule.getProductName() != null){
            entity.setProductName(rule.getProductName());
        }
        if(rule.getProductCode() != null){
            entity.setProductCode(rule.getProductCode());
        }
        if(rule.getProductImage() != null){
            entity.setProductImage(rule.getProductImage());
        }
        if(rule.getProductPrice() != null){
            entity.setProductPrice(rule.getProductPrice());
        }
        if(rule.getProductPriceUnit() != null){
            entity.setProductPriceUnit(rule.getProductPriceUnit());
        }
        if(rule.getProductWeight() != null){
            entity.setProductWeight(rule.getProductWeight());
        }
        if(rule.getProductWeightUnit() != null){
            entity.setProductWeightUnit(rule.getProductWeightUnit());
        }
        if(rule.getProductAttribute() != null){
            entity.setProductAttribute(rule.getProductAttribute());
        }
        if(rule.getProductAttributeValue() != null){
            entity.setProductAttributeValue(rule.getProductAttributeValue());
        }
        if(rule.getProductDescription() != null){
            entity.setProductDescription(rule.getProductDescription());
        }
        entity.setModifyTime(new Date());
        this.save(entity);
    }
}
